// const { ajax } = require("jquery");

// const { ajax } = require("jquery")

// const { ajax } = require("jquery")

// const { ajax } = require("jquery");




  
let c_x = async() =>{
  let res_err = await ajax({
    type: "GET",
    url:'http://127.0.0.1:4000/collections/GetCollection'
})
let res_F = JSON.parse(res_err)
let url = window.location.search
let sut_id = url.slice(url.indexOf("=")+1)
let new_arr = res_F.obj.filter(v=>{

 return v.studentId._id == sut_id
})
xuan1(new_arr)
$('.page li:eq(1)').html(`我的收藏（${new_arr.length}）`)
}

 
   



let getErr = async()=>{
  let res_err = await ajax({
      type: "GET",
      url:'http://127.0.0.1:4000/collections/GetCollection'
  })
  let res_F = JSON.parse(res_err)
  let url = window.location.search
  let sut_id = url.slice(url.indexOf("=")+1)
  let new_arr = res_F.obj.filter(v=>{
  
   return v.studentId._id == sut_id
  })
 
  var swiper = new Swiper(".mySwiper", {
    pagination: {
      el: ".swiper-pagination",
      clickable: true,
      renderBullet: function (index, className) {
        return '<span class="' + className + '">' + (index + 1) + "</span>";
      },
    },
  });

  if(new_arr.length>=1){
    
    xuan1(new_arr)
  }else{
    let span = `<p class="nb">没有收藏，快去做题吧</p>`
    $('.tab li:eq(1)').find(".sw1").append(span)
    
    $('.tab li:eq(1)').find(".nb").css({
      margin:'50px 0',
         width: '100%',
      fontSize:"20px",
     textAlign:"center",
    })
  }
  
  
  $('.page li:eq(1)').html(`我的收藏（${new_arr.length}）`)

  let res_err2 = await ajax({
     type: "GET",
     url:'http://127.0.0.1:4000/testPaper/testPaperErr'
  })
 
  let res_F2 = JSON.parse(res_err2)
  let url2 = window.location.search
  let sut_id2 = url2.slice(url2.indexOf("=")+1)
  let new_arr2 = res_F2.obj.filter(v=>{
  
   return v.studentId._id == sut_id2
  })

  var swiper = new Swiper(".mySwiper2", {
    pagination: {
      el: ".swiper-pagination",
      clickable: true,
      renderBullet: function (index, className) {
        return '<span class="' + className + '">' + (index + 1) + "</span>";
      },
    },
  });
  

if(new_arr2.length >=1){
  xuan2(new_arr2)
}else{
  let span = `<p class="nb">没有错题，快去做题吧</p>`
    $('.tab li:eq(2)').find(".sw2").append(span)
    
    $('.tab li:eq(2)').find(".nb").css({
      margin:'50px 0',
         width: '100%',
      fontSize:"20px",
     textAlign:"center",
    })
}

 
  $('.page li:eq(2)').html(`错题库（${new_arr2.length}）`)

  setTimeout(function () {
    let stu_id = window.location.search.slice(window.location.search.indexOf("=")+1)
    if(localStorage.getItem(`collect${stu_id}`)){
      let arr = JSON.parse(localStorage.getItem(`collect${stu_id}`))
    
      arr.forEach(v=>{
        $(".btn_sxxx").each((i,e)=>{
              if(v==e.getAttribute('index')){
               
                e.innerText ="已收藏"
              }
        })
      })
    
    }
  },50)
 

 $('.btn_sxxx').on('click', function(){
  
   if(this.innerText=="收藏"){
     let stu_id = window.location.search.slice(window.location.search.indexOf("=")+1)
     let err_id = $(this).attr('index')
     if(!localStorage.getItem(`collect${stu_id}`)){
      let arr = []
      arr.push(err_id)
      localStorage.setItem(`collect${stu_id}`,JSON.stringify(arr))
      $(this).html("已收藏")
     }else{
      let old =JSON.parse(localStorage.getItem(`collect${stu_id}`)) 
      old.push(err_id)
      let set = new Set(old)
      let new_arr = [...set]
      localStorage.setItem(`collect${stu_id}`,JSON.stringify(new_arr))
       $(this).html("已收藏")
      
    }
     
    


   
    if(localStorage.getItem(`collect${stu_id}`)){
      let arr = JSON.parse(localStorage.getItem(`collect${stu_id}`))
    
      arr.forEach(v=>{
        $(".btn_sxxx").each((i,e)=>{
              if(v==e.getAttribute('index')){
               
                e.innerText ="已收藏"
              }
        })
      })
    
    }

   ajax({
     type: "post",
     url:'http://127.0.0.1:4000/collections/CreatCollection',
     data:{
       studentId:stu_id, 
       exerciseId:err_id,
     }
   })

   c_x()

   }else{
    
    let stu_id = window.location.search.slice(window.location.search.indexOf("=")+1)
    let err_id = $(this).attr('index')
    let old =JSON.parse(localStorage.getItem(`collect${stu_id}`)) 
     
    old.splice(old.indexOf(err_id),1)
    
    localStorage.setItem(`collect${stu_id}`,JSON.stringify(old))
    $(this).html("收藏")
    ajax({
      type: "post",
      url:'http://127.0.0.1:4000/collections/DeleteCollection',
      data:{
        studentId:stu_id, 
        exerciseId:err_id,
      }
    })
    c_x()
   }
   
  
 })




}


function xuan2(arr){
  function str2(str){
  
    if(str.length<=1){
      return JSON.parse(str)
    }else{
     
      return str
    }
 }

 function zhuan(str){
    if(str=="0"){
      return "A"
    }else if(str=="1"){
      return "B"
    }else if(str=="2"){
      return "C"
    }
    else if(str=="3"){
      return "D"
    }
 }
 
  let str =""
  arr.forEach(v=>{
  
    
    let item  = `
    <div  class="swiper-slide">
    <div class="cuoti_k">
      <div class="swiper_p">
        <p>${v.exerciseId.topics}</p>
      </div>

      <div class="swiper_xuanze">
        <div index=0 class="a_ss">A</div>
        <div class="danan_swiper">${str2(v.exerciseId.options)[0]}</div>
      </div>

      <div class="swiper_xuanze">
        <div index=1 class="a_ss">B</div>
        <div class="danan_swiper">${str2(v.exerciseId.options)[1]}</div>
      </div>

      <div class="swiper_xuanze">
        <div index=2 class="a_ss">C</div>
        <div class="danan_swiper">${str2(v.exerciseId.options)[2]}</div>
      </div>

      <div class="swiper_xuanze">
        <div index=3 class="a_ss">D</div>
        <div class="danan_swiper">${str2(v.exerciseId.options)[3]}</div>
      </div>


      <div class="sc_btn">
        <div index=${v.exerciseId._id} class="btn_sxxx">
          收藏
        </div>

      </div>


      <div class="jx_btn_da">
      <div class="btn_your_zq">  <span style='font-weight:bold;color:red'>你的答案:</span>  <span index=${v.errorAnswer}  style='font-size:17px;color:red' class="btn_your_zq_sp ddr">${zhuan(v.errorAnswer)}</span></div>
        <div class="btn_your_zq">  <span style='font-weight:bold'>正确答案:</span>  <span index=${v.exerciseId.answer}  style='font-size:17px' class="btn_your_zq_sp ann">${zhuan(v.exerciseId.answer)}</span></div>
        <div  class="btn_your_jx"> <span style='font-weight:bold'>解析 :</span>  
          <p style="margin-top: 10px" class="jxjx">${v.exerciseId.analysis}</p>
        </div>
      </div>
    </div>
  </div>
    
    `
   
   str += item
  })
  
  document.querySelector('.sw2').innerHTML = str

 
  $('.ddr').each((i,v)=>{
     $(v).parents(".swiper-slide").find(`.a_ss[index=${v.getAttribute('index')}]`).css({
       background:'red',
       color:'white'
     })
  })

  $('.ann').each((i,v)=>{
    $(v).parents(".swiper-slide").find(`.a_ss[index=${v.getAttribute('index')}]`).css({
      background:'#007aff',
      color:'white'
    })
 })
}




function xuan1(arr){

  function zhuan(str){
    if(str=="0"){
      return "A"
    }else if(str=="1"){
      return "B"
    }else if(str=="2"){
      return "C"
    }
    else if(str=="3"){
      return "D"
    }
 }

  function str2(str){
  
    if(str.length<=1){
      return JSON.parse(str)
    }else{
     
      return str
    }
 }
 
  let str =""
  arr.forEach(v=>{
   
   
    let item  = `
    <div index=${v.exerciseId._id} class="swiper-slide">
    <div class="cuoti_k">
      <div class="swiper_p">
        <p>${v.exerciseId.topics}</p>
      </div>

      <div class="swiper_xuanze">
        <div index=0 class="a_ss">A</div>
        <div class="danan_swiper">${str2(v.exerciseId.options)[0]}</div>
      </div>

      <div class="swiper_xuanze">
        <div index=1 class="a_ss">B</div>
        <div class="danan_swiper">${str2(v.exerciseId.options)[1]}</div>
      </div>

      <div class="swiper_xuanze">
        <div index=2 class="a_ss">C</div>
        <div class="danan_swiper">${str2(v.exerciseId.options)[2]}</div>
      </div>

      <div class="swiper_xuanze">
        <div index=3 class="a_ss">D</div>
        <div class="danan_swiper">${str2(v.exerciseId.options)[3]}</div>
      </div>


      <div class="sc_btn">
       

      </div>


      <div class="jx_btn_da">
      
        <div class="btn_your_zq">  <span style='font-weight:bold'>正确答案:</span>  <span  style='font-size:17px' index=${v.exerciseId.answer}  class="btn_your_zq_sp ann2">${zhuan(v.exerciseId.answer)}</span></div>
        <div  class="btn_your_jx"> <span style='font-weight:bold'>解析 :</span>  
          <p style="margin-top: 10px" class="jxjx">${v.exerciseId.analysis}</p>
        </div>
      </div>
    </div>
  </div>
    
    `
   
   str += item
  })
  
  document.querySelector('.sw1').innerHTML = str

  $('.ann2').each((i,v)=>{
    $(v).parents(".swiper-slide").find(`.a_ss[index=${v.getAttribute('index')}]`).css({
      background:'#007aff',
      color:'white'
    })
  
 })
  

}


setTimeout(function(){
  getErr()
},40) 




